package com.jackiechancinema.ssm.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.ContractDao;
import com.jackiechancinema.ssm.dao.CustomerDao;
import com.jackiechancinema.ssm.dto.ContractExcelDto;
import com.jackiechancinema.ssm.dto.CustomerExcelDto;
import com.jackiechancinema.ssm.entity.Contract;
import com.jackiechancinema.ssm.entity.Customer;
import com.jackiechancinema.ssm.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerReadListener extends AnalysisEventListener<CustomerExcelDto> {
    @Autowired
    private CustomerDao dao;

    private List<Customer> list=new ArrayList<>();
    //获取读取到的每一行对应的对象
    @Override
    public void invoke(CustomerExcelDto dto, AnalysisContext analysisContext) {
        System.out.println(dto);
        //实现属性的赋值
        if (dto != null){
            Customer customer = new Customer();
            if (StringUtils.hasLength(dto.getCustomer_name())){
                customer.setCustomer_name(dto.getCustomer_name());
            }
            if (StringUtils.hasLength(dto.getCustomer_mobile())){
                customer.setCustomer_mobile(dto.getCustomer_mobile());
            }
            if (StringUtils.hasLength(dto.getCustomer_email())){
                customer.setCustomer_email(dto.getCustomer_email());
            }
            if (SystemContant.CLASS_NAME_YEAR.equals(dto.getClass_name())){
                customer.setClass_id(SystemContant.CLASS_ID_YEAR);
            }else if (SystemContant.CLASS_NAME_MONTH.equals(dto.getClass_name())){
                customer.setClass_id(SystemContant.CLASS_ID_MONTH);
            }else if (SystemContant.CLASS_NAME_TRY.equals(dto.getClass_name())){
                customer.setClass_id(SystemContant.CLASS_ID_TRY);
            }
            System.err.println(customer);
            //添加到集合
            list.add(customer);
        }

    }
    //整个读取结束
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //将读取的数据一次性的存储到数据库
        int r=dao.insertBatch(list);
        list.clear();
    }
}
